package com.stationbuilding.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.stationbuilding.R;
import com.stationbuilding.adapter.FragmentPagerAdapter;
import com.stationbuilding.api.RetrofitManager;
import com.stationbuilding.bean.QuestionFlowListBean;
import com.stationbuilding.bean.ResultBean;
import com.stationbuilding.fragment.safe.DetailsFragment;
import com.stationbuilding.fragment.safe.FlowFragment;
import com.stationbuilding.utils.DialogUtils;
import com.stationbuilding.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.net.ssl.HandshakeCompletedListener;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MultipartBody;

public class ShowActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> titleList = Arrays.asList("信息", "流程");
    private String token = PreferenceHelper.getInstance(this).getToken();
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        // 初始化toolbar
        initToolbar();
        initView();
        changeStatus();
    }

    private void initToolbar() {
        // 点击事件监听监听
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.back:
                        finish();
                        break;
                    case R.id.button:
                        startActivity(new Intent(ShowActivity.this, HandleActivity.class));
                        break;
                }
            }
        };
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        ImageView back = (ImageView) toolbar.findViewById(R.id.back);
        Button button = (Button) toolbar.findViewById(R.id.button);
        TextView title = (TextView) toolbar.findViewById(R.id.title);
        title.setText(getResources().getText(R.string.safe_question));
        button.setText(getResources().getString(R.string.handle));
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(onClickListener);
        back.setOnClickListener(onClickListener);
    }

    private void initView() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new DetailsFragment());
        fragmentList.add(new FlowFragment());
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        // 将ViewPager关联到TabLayout上
        tabLayout.setupWithViewPager(viewPager);

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
    }

    public View getTabView(int position) {
        View view;
        if (position == 0) {
            view = LayoutInflater.from(this).inflate(R.layout.flow_tab_item_left, null);
        } else {
            view = LayoutInflater.from(this).inflate(R.layout.flow_tab_item_right, null);
        }
        TextView tv = (TextView) view.findViewById(R.id.title);
        tv.setText(titleList.get(position));
        return view;
    }

    private void changeStatus() {
        int status = getIntent().getIntExtra("status", 1);
        if (status == 0) {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            // ID
            builder.addFormDataPart("id", getIntent().getStringExtra("id"));
            // status
            builder.addFormDataPart("status", "1");
            builder.setType(MultipartBody.FORM);
            MultipartBody multipartBody = builder.build();
            RetrofitManager.toSubscribe(RetrofitManager.getApiService().updateQuestionFlow(token, multipartBody), new Observer<ResultBean>() {
                @Override
                public void onComplete() {
                }
                @Override
                public void onError(Throwable e) {
                    Toast.makeText(ShowActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
                }
                @Override
                public void onNext(ResultBean resultBean) {
                    if (resultBean.getCode() != 0) {
                        Toast.makeText(ShowActivity.this, "更改已读状态失败:" + resultBean.getCode(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}
